﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using Object = UnityEngine.Object;

namespace CornerMemo
{
    /// <summary>
    /// UI的列表管理
    /// </summary>
    /// <typeparam name="TData">数据类型</typeparam>
    /// <typeparam name="TItem">项类型</typeparam>
    public class UIItemListManager<TData, TItem>
    {
        public List<TData> DataList;
        public List<TItem> ItemList;
        public Transform ItemParent;
        public GameObject ItemPrefab;

        private Action<TData, TItem> _showAction;
        private Action<TItem> _hideAction;
        private Action _onRefreshSuccess;
        public UIItemListManager(Transform itemParent, GameObject itemPrefab,List<TData> dataList, Action<TData, TItem> showAction = null,
            Action<TItem> hideAction = null, Action onRefreshSuccess = null)
        {
            ItemList = new List<TItem>();
            DataList = dataList;
            ItemParent = itemParent;
            ItemPrefab = itemPrefab;
            _showAction = showAction;
            _hideAction = hideAction;
            _onRefreshSuccess = onRefreshSuccess;
        }

        /// <summary>
        /// 刷新页面列表
        /// </summary>
        /// <param name="showAction">有数据时显示列表动作</param>
        /// <param name="hideAction">没数据时隐藏列表动作</param>
        /// <param name="dataList">列表数据</param>
        public void RefreshPage(Action<TData, TItem> showAction = null,Action<TItem> hideAction = null, List<TData> dataList = null)
        {
            if (dataList != null)//如果传入的数据不为空，使用当前的数据列表
                DataList = dataList;
            if (hideAction != null)
                _hideAction = hideAction;
            if (showAction != null)//如果传入的显示动作为空，使用当前的显示动作
                _showAction = showAction;
            
            if (DataList.Count == 0)//数据列表为空时，隐藏所有项
            {
                foreach (var item in ItemList)
                {
                    _hideAction.Invoke(item);
                }
                _onRefreshSuccess?.Invoke();
                return;
            }
            
            for (int i = 0; i < DataList.Count; i++)//数据列表不为空时，显示所有数据项
            {
                var index = i;
                var data = DataList[i];
                if (index < ItemList.Count)//如果数据长度小于当前已创建的Item时直接复用
                {
                    var item = ItemList[index];
                    _showAction.Invoke(data, item);
                }
                else//否则创建新的Item
                {
                    var item = Object.Instantiate(ItemPrefab, ItemParent).GetComponent<TItem>();
                    _showAction.Invoke(data, item);
                    ItemList.Add(item);
                }
            }

            if (ItemList.Count > DataList.Count)
            {
                //如果数据长度大于当前已创建的Item时，隐藏多余的Item
                for (int i = ItemList.Count - 1; i >= DataList.Count; i--)
                {
                    var item = ItemList[i];
                    _hideAction?.Invoke(item);
                }
            } 
            _onRefreshSuccess?.Invoke();
        }
        
        public void Clear()
        {
            if (ItemParent && ItemParent.childCount > 0 )
            {
                ItemParent.ClearChildAll();
            }
            ItemList = new List<TItem>();
            ItemParent = null;
            ItemPrefab = null;
        }
    }
}